﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.Utils;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class PR_ScoreMain : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        RadComboBox empCombo = new RadComboBox();
        SystemYearTypeBLL yearTypeBLL = new SystemYearTypeBLL();
        ScoreMainBLL obj = new ScoreMainBLL();
        IEmployeeTypeBLL etBLL = new EmployeeTypeBLL();

        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!Page.IsPostBack)
            {
                bindEmpType1();
                bindYearCombo();
                cbYear_SelectedIndexChanged(null, null);                               
            }
        }
       private void LoadDataGridOverLoad()
        {
            try
            {
                if (radPeriod.SelectedValue == null)
                    return;

                if (cbEmployeeType.SelectedIndex == -1)
                    return;

                string StdSquence = obj.GetAppraisalSequence(int.Parse(cbYear.SelectedValue)
                    , int.Parse(radPeriod.SelectedValue)
                    , int.Parse(cbEmployeeType.SelectedValue));
                if (StdSquence == null ) {
                    RadGrid1.DataSource = new DataTable();
                }else{
                    DataTable dt = obj.LoadStdScoreDtls((int?)null, Int32.Parse(StdSquence));
                    RadGrid1.DataSource = dt;
                }
                
                RadGrid1.DataBind();                

            }
            catch(Exception ex)
            {

            }
        }

        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "Delete")
                {
                    int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["std_dtls_seq"]);
                    obj.Delete(seq, ssMng.EmpSeqI);
                }
                if (e.CommandName == "ExpandCollapse")
                    return;

                LoadDataGridOverLoad();

                Session["APRSEQ"] = obj.GetAppraisalSequence(int.Parse(cbYear.SelectedValue)
                    , int.Parse(radPeriod.SelectedValue)
                    , int.Parse(cbEmployeeType.SelectedValue));                
            }
            catch (Exception ex)
            {
                Web.ShowAlert(this, ex.Message);
            }
        }

        private void bindEmpType1()
        {
            IList<EmployeeTypeInfo> info = etBLL.ListEmployeeTypes();

            cbEmployeeType.DataSource = info;
            cbEmployeeType.DataTextField = "Name";
            cbEmployeeType.DataValueField = "Sequence";
            cbEmployeeType.DataBind();
            cbEmployeeType.SelectedIndex = 0;
            cbEmployeeType_SelectedIndexChanged(null, null);
        }

        public void bindYearCombo()
        {
            int curYear = int.Parse(DateTime.Today.Year.ToString());
            curYear += 543;
            int MinYear = curYear - 10;
            int MaxYear = curYear + 10;
            DataTable dt = new DataTable();
            DataColumn dc = dt.Columns.Add("Name");

            for (int i = MinYear; i <= MaxYear; i++)
            {
                dt.Rows.Add(Convert.ToString(i));
            }
            cbYear.DataSource = dt;
            cbYear.DataBind();
            cbYear.SelectedValue = curYear.ToString();
            cbYear_SelectedIndexChanged(null, null);
        }

        protected void cbYear_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {          
            LoadDataGridOverLoad();
        }

        protected void cbEmployeeType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadDataGridOverLoad();
        }

        protected void radPeriod_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDataGridOverLoad();
        }

    }
}
